package com.example;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class Client {


    /**
     * 上行路线
     */
    private static List<Site> upRouteList = new ArrayList<>(15);

    /**
     * 下行路线
     */
    private static List<Site> downRouteList = new ArrayList<>(15);

    private static Site upRouteStartSite = null;
    private static Site upRouteEndSite = null;
    private static Site downRouteStartSite = null;
    private static Site downRouteEndSite = null;

    public static void main(String[] args) {

        initSite();

        //上行出发的五辆
        Bus busA = new Bus("A", upRouteStartSite, 0 * 60);
        Bus busB = new Bus("B", upRouteStartSite, 15 * 60);
        Bus busC = new Bus("C", upRouteStartSite, 30 * 60);
        Bus busD = new Bus("D", upRouteStartSite, 45 * 60);
        Bus busE = new Bus("E", upRouteStartSite, 60 * 60);

        //下行出发的五辆
        Bus busF = new Bus("F", upRouteStartSite, 0 * 60);
        Bus busG = new Bus("G", upRouteStartSite, 15 * 60);
        Bus busH = new Bus("H", upRouteStartSite, 30 * 60);
        Bus busI = new Bus("I", upRouteStartSite, 45 * 60);
        Bus busJ = new Bus("J", upRouteStartSite, 60 * 60);

        //生成乘客
        busA.start();
        busB.start();
        busC.start();
        busD.start();
        busE.start();

        busF.start();
        busG.start();
        busH.start();
        busI.start();
        busJ.start();

        while (true) {
            try {
                Thread.sleep(5 * 60 * 10L);
                generatorGuest();
                upRouteList.stream().forEach(System.out::println);
                downRouteList.stream().forEach(System.out::println);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


    }

    /**
     * 生成乘客
     */
    private static void generatorGuest() {
        Random random = new Random();
        //生成10名乘客
        for (int i = 0; i < 10; i++) {
            Guest guest = new Guest();
            //选择上行还是下行
            int selectUpOrDown = random.nextInt(2);
            //随机初始站点（终点除外）
            //01-14
            int startSiteIndex = random.nextInt(14);
            //确定目标站点
            int targetSiteIndex = random.nextInt(14 - startSiteIndex) + startSiteIndex + 1;
            switch (selectUpOrDown) {
                //上行
                case 0:
                    upRouteList.get(startSiteIndex).addGuest(guest);
                    guest.setStartSite(upRouteList.get(startSiteIndex));
                    guest.setTargetSite(upRouteList.get(targetSiteIndex));
                    break;
                //下行
                case 1:
                    downRouteList.get(startSiteIndex).addGuest(guest);
                    guest.setStartSite(downRouteList.get(startSiteIndex));
                    guest.setTargetSite(downRouteList.get(targetSiteIndex));
                    break;
                default:
                    System.err.println("异常出现");
                    break;
            }
        }
    }


    /**
     * 初始化站点
     */
    private static void initSite() {
        Site upRoute01 = new Site("01");
        Site upRoute02 = new Site("02");
        Site upRoute03 = new Site("03");
        Site upRoute04 = new Site("04");
        Site upRoute05 = new Site("05");
        Site upRoute06 = new Site("06");
        Site upRoute07 = new Site("07");
        Site upRoute08 = new Site("08");
        Site upRoute09 = new Site("09");
        Site upRoute10 = new Site("10");
        Site upRoute11 = new Site("11");
        Site upRoute12 = new Site("12");
        Site upRoute13 = new Site("13");
        Site upRoute14 = new Site("14");
        Site upRoute15 = new Site("15");

        Site downRoute01 = new Site("01");
        Site downRoute02 = new Site("02");
        Site downRoute03 = new Site("03");
        Site downRoute04 = new Site("04");
        Site downRoute05 = new Site("05");
        Site downRoute06 = new Site("06");
        Site downRoute07 = new Site("07");
        Site downRoute08 = new Site("08");
        Site downRoute09 = new Site("09");
        Site downRoute10 = new Site("10");
        Site downRoute11 = new Site("11");
        Site downRoute12 = new Site("12");
        Site downRoute13 = new Site("13");
        Site downRoute14 = new Site("14");
        Site downRoute15 = new Site("15");

        upRoute01.setNextSite(upRoute02, 5 * 60);
        upRoute02.setNextSite(upRoute03, 6 * 60);
        upRoute03.setNextSite(upRoute04, 7 * 60);
        upRoute04.setNextSite(upRoute05, 8 * 60);
        upRoute05.setNextSite(upRoute06, 4 * 60);
        upRoute06.setNextSite(upRoute07, 3 * 60);
        upRoute07.setNextSite(upRoute08, 6 * 60);
        upRoute08.setNextSite(upRoute09, 5 * 60);
        upRoute09.setNextSite(upRoute10, 6 * 60);
        upRoute10.setNextSite(upRoute11, 7 * 60);
        upRoute11.setNextSite(upRoute12, 4 * 60);
        upRoute12.setNextSite(upRoute13, 3 * 60);
        upRoute13.setNextSite(upRoute14, 6 * 60);
        upRoute14.setNextSite(upRoute15, 3 * 60);
        upRoute15.setNextSite(downRoute15, 0 * 60);

        downRoute15.setNextSite(downRoute14, 4 * 60);
        downRoute14.setNextSite(downRoute13, 5 * 60);
        downRoute13.setNextSite(downRoute12, 4 * 60);
        downRoute12.setNextSite(downRoute11, 5 * 60);
        downRoute11.setNextSite(downRoute10, 4 * 60);
        downRoute10.setNextSite(downRoute09, 7 * 60);
        downRoute09.setNextSite(downRoute08, 3 * 60);
        downRoute08.setNextSite(downRoute07, 5 * 60);
        downRoute07.setNextSite(downRoute06, 4 * 60);
        downRoute06.setNextSite(downRoute05, 3 * 60);
        downRoute05.setNextSite(downRoute04, 6 * 60);
        downRoute04.setNextSite(downRoute03, 5 * 60);
        downRoute03.setNextSite(downRoute02, 7 * 60);
        downRoute02.setNextSite(downRoute01, 4 * 60);
        downRoute01.setNextSite(upRoute01, 0 * 60);

        upRouteList.add(upRoute01);
        upRouteList.add(upRoute02);
        upRouteList.add(upRoute03);
        upRouteList.add(upRoute04);
        upRouteList.add(upRoute05);
        upRouteList.add(upRoute06);
        upRouteList.add(upRoute07);
        upRouteList.add(upRoute08);
        upRouteList.add(upRoute09);
        upRouteList.add(upRoute10);
        upRouteList.add(upRoute11);
        upRouteList.add(upRoute12);
        upRouteList.add(upRoute13);
        upRouteList.add(upRoute14);
        upRouteList.add(upRoute15);

        downRouteList.add(downRoute15);
        downRouteList.add(downRoute14);
        downRouteList.add(downRoute13);
        downRouteList.add(downRoute12);
        downRouteList.add(downRoute11);
        downRouteList.add(downRoute10);
        downRouteList.add(downRoute09);
        downRouteList.add(downRoute08);
        downRouteList.add(downRoute07);
        downRouteList.add(downRoute06);
        downRouteList.add(downRoute05);
        downRouteList.add(downRoute04);
        downRouteList.add(downRoute03);
        downRouteList.add(downRoute02);
        downRouteList.add(downRoute01);

        upRouteStartSite = upRoute01;
        upRouteEndSite = upRoute15;

        downRouteStartSite = downRoute15;
        downRouteEndSite = downRoute01;
    }
}
